<?php


/* 
 * The include path may change 
 * depending on PHPCodeProvider.php location. 
 */

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromExpression call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the BinaryOperatorExpression class.
 * BinaryOperatorExpression is a child of the Expression class
 * so it can be used as an argument for 
 * PHPCodeProvider::generateCodeFromExpression()
 */

$expression = new BinaryOperatorExpression(BinaryOperator::ASSIGN);

/*
 * The left side of $expression will be a reference to the 'score'
 * property of the current class.
 */

$expression->Left = 
    new FieldReferenceExpression('score', new ThisReferenceExpression());

/*
 * The right side of $expression will create an initialize an array
 */

$expression->Right = new ArrayCreateExpression();

/*
 * The first element of the array
 */

$expression->Right->Initializers[0] = new BinaryOperatorExpression(BinaryOperator::KEY_VALUE);
$expression->Right->Initializers[0]->Left = new PrimitiveExpression('won');
$expression->Right->Initializers[0]->Right = new PrimitiveExpression(0);

/*
 * The second element
 */

$expression->Right->Initializers[1] = new BinaryOperatorExpression(BinaryOperator::KEY_VALUE);
$expression->Right->Initializers[1]->Left = new PrimitiveExpression('lost');
$expression->Right->Initializers[1]->Right = new PrimitiveExpression(0);

/*
 * Create a PHPCodeProvider instance and execute generateCodeFromExpression()
 * with $expression as the argument
 */

$provider = new PHPCodeProvider();
$provider->generateCodeFromExpression($expression, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be :
 * (no semicolon at the end because this is an expression, not a statement)
 * 
 * $this->score = array('won' => 0, 'lost' => 0)
 */

?>
